library(survey) 
library(car)
library(ggplot2)
library(gridExtra)

# Bring in survey data
dat <- read.csv("YouGov-PLMX0196.csv")

# Recode variables
dat$presvote16x[dat$presvote16x==3] <- NA
dat$presvote16x <- as.factor(dat$presvote16x)
dat$presvote16x <- relevel(dat$presvote16x,ref="4")

dat$highinterest[dat$newsint>1] <- 0
dat$highinterest[dat$newsint==1] <- 1

dat$trumpapproval <- recode(dat$app_dtrmp, "1=5; 2=4; 5=3; 3=2; 4=1")
dat$trumpapproval <- as.factor(dat$trumpapproval)

dat$strongrep[dat$pid7==7] <- 1
dat$strongrep[dat$pid7<7] <- 0

dat$strongdem[dat$pid7>1] <- 0
dat$strongdem[dat$pid7==1] <- 1

dat$age <- 2016 - dat$birthyr
dat$age4 <- recode(dat$age, "18:29=1; 30:44=2; 45:64=3; else=4")

dat$educ4 <- recode(dat$educ, "1:2=1; 3:4=2; 5=3; 6=4")
dat$educ3 <- recode(dat$educ4, "4=3")
dat$educ2 <- recode(dat$educ, "1:4=1; 5:6=2")
dat$educ2f <- factor(dat$educ2, labels = c("Some college-", "College grad+"))

dat$eduint <- 9
dat$eduint[dat$highinterest == 0 & dat$educ2 == 1] <- 1
dat$eduint[dat$highinterest == 0 & dat$educ2 == 2] <- 2
dat$eduint[dat$highinterest == 1 & dat$educ2 == 1] <- 3
dat$eduint[dat$highinterest == 1 & dat$educ2 == 2] <- 4
dat$eduintf <- factor(dat$eduint, labels = c("Low int, low educ", "Low int, high educ",
    "High int, low educ", "High int, high educ"))

# Create the survey object for analysis
svy.dat <- svydesign(ids = ~1, data = dat, weights = dat$weight) 

# Create various subsets
people <- subset(dat,dat$Q1!=9)
whichprez <- subset(dat, dat$Q2!=9)

# Additional recodings
people$Q1[people$Q1==2] <- 0
whichprez$Q2[whichprez$Q2==2] <- 0

# Create survey objects for subsets
svy.people <- svydesign(ids = ~1, data = people, weights = people$weight) 
svy.whichprez <- svydesign(ids = ~1, data = whichprez, weights = whichprez$weight) 

# Relationship between incorrect answers and 2016 vote choice
incorrectbyvote1 <- prop.table(svytable(~Q1+presvote16x, design = svy.people))
incorrectbyvote1 <- prop.table(incorrectbyvote1, 2)
svychisq(~Q1+presvote16x, design = svy.people, statistic="adjWald")

incorrectbyapp1 <- prop.table(svytable(~Q1+trumpapproval, design = svy.people))
incorrectbyapp1 <- prop.table(incorrectbyapp1, 2)
svychisq(~Q1+trumpapproval, design = svy.people, statistic="adjWald")

# Plot results for crowd size question (Figure 2 in article)
tographq1a <- as.data.frame(incorrectbyvote1)
tographq1a <- subset(tographq1a, Q1==1)
fig1a <- ggplot(tographq1a, aes(x = presvote16x, y = Freq*100))
fig1a <- fig1a + geom_bar(stat = "identity", fill="black") + theme_bw() + scale_y_continuous(limits = c(0, 20))
fig1a <- fig1a + scale_fill_brewer(palette="Set1",guide = guide_legend(title = NULL))
fig1a <- fig1a + xlab("2016 Presidential Vote") + ylab("Percent choosing wrong photo")
fig1a <- fig1a + scale_x_discrete(breaks=c(4,1,2),
                         labels=c("Non- \nvoters", "Clinton \nVoters", "Trump \nVoters"))
fig1a <- fig1a + geom_text(aes(label=paste(round(100*Freq,0),"%",sep="")), vjust=-0.5, 
          position=position_dodge(.9),color="black")

tographq1b <- as.data.frame(incorrectbyapp1)
tographq1b <- subset(tographq1b, Q1==1)
fig1b <- ggplot(tographq1b, aes(x = trumpapproval, y = Freq*100))
fig1b <- fig1b + geom_bar(stat = "identity", fill="black") + theme_bw() + scale_y_continuous(limits = c(0, 20))
fig1b <- fig1b + scale_fill_brewer(palette="Set1",guide = guide_legend(title = NULL))
fig1b <- fig1b + xlab("Trump Approval") + ylab("Percent choosing wrong photo")
fig1b <- fig1b + scale_x_discrete(breaks=c(1,2,3,4,5),
                                  labels=c("Strongly \ndisapprove", "Somewhat \ndisapprove", "Not \nsure", "Somewhat \napprove", "Strongly \napprove"))
fig1b <- fig1b + geom_text(aes(label=paste(round(100*Freq,0),"%",sep="")), vjust=-0.5, 
                         position=position_dodge(.9),color="black")

setEPS()
postscript(file="fig2.eps", width=8, height=4)
grid.arrange(fig1a, fig1b, ncol=2)
dev.off()


# Graph how many people question by education & presidential vote/approval
byeduc <- svyby(~Q1, ~presvote16x + educ2, svy.people, svymean, keep.var=TRUE)
byeduc <- as.data.frame(byeduc)
svychisq(~Q1+educ2, design = subset(svy.people, presvote16x==2), statistic="adjWald")

fig3b <- ggplot(data=byeduc, aes(x=presvote16x, y=Q1*100, fill=as.factor(educ2))) +
  geom_bar(stat="identity", position=position_dodge())
fig3b <- fig3b + theme_bw() + scale_y_continuous(limits = c(0, 30))
fig3b <- fig3b + xlab("2016 Presidential Vote") + ylab("Percent choosing wrong photo")
fig3b <- fig3b + scale_x_discrete(breaks=c(4,1,2),
                                labels=c("Non- \nvoters", "Clinton \nVoters", "Trump \nVoters"))
fig3b <- fig3b + scale_fill_manual(name="",
                                   breaks=c(1, 2),
                                   labels=c("Low education", "High education"), values=c("#999999","#000000"))
fig3b <- fig3b + geom_text(aes(label=paste(round(100*Q1,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")


# Graph how many people question by interest in politics & presidential approval
byeduc <- svyby(~Q1, ~trumpapproval + educ2, svy.people, svymean, keep.var=TRUE)
byeduc <- as.data.frame(byeduc)
svychisq(~Q1+educ2, design = subset(svy.people, trumpapproval==5), statistic="adjWald")


fig3c <- ggplot(data=byeduc, aes(x=trumpapproval, y=Q1*100, fill=as.factor(educ2))) +
  geom_bar(stat="identity", position=position_dodge())
fig3c <- fig3c + theme_bw() + scale_y_continuous(limits = c(0, 30))
fig3c <- fig3c + xlab("Trump approval") + ylab("Percent choosing wrong photo")
fig3c <- fig3c + scale_x_discrete(breaks=c(1,2,3,4,5),
                                  labels=c("Strongly \ndisapprove", "Somewhat \ndisapprove", "Not \nsure", "Somewhat \napprove", "Strongly \napprove"))
fig3c <- fig3c + scale_fill_manual(name="",
                                     breaks=c(1, 2),
                                     labels=c("Low education", "High education"), values=c("#999999","#000000")) 
fig3c <- fig3c + geom_text(aes(label=paste(round(100*Q1,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black") 

## Collect graphs to produce Figure 3 in article
setEPS()
postscript(file="fig3.eps", width=12, height=4)
grid.arrange(fig3b, fig3c, ncol=2)
dev.off()

# Graph how many people question by interest in politics & presidential vote
byinterest <- svyby(~Q1, ~presvote16x + highinterest, svy.people, svymean, keep.var=TRUE)
byinterest <- as.data.frame(byinterest)

fig3a <- ggplot(data=byinterest, aes(x=presvote16x, y=Q1*100, fill=as.factor(highinterest))) +
  geom_bar(stat="identity", position=position_dodge())
fig3a <- fig3a + theme_bw() + scale_y_continuous(limits = c(0, 30))
fig3a <- fig3a + xlab("2016 Presidential Vote") + ylab("Percent choosing wrong photo")
fig3a <- fig3a + scale_x_discrete(breaks=c(4,1,2),
                                  labels=c("Non- \nvoters", "Clinton \nVoters", "Trump \nVoters"))
fig3a <- fig3a + scale_fill_manual(name="",
                                   breaks=c(0, 1),
                                   labels=c("Low interest", "High interest"), values=c("#999999","#000000"))
fig3a <- fig3a + geom_text(aes(label=paste(round(100*Q1,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")

# Graph how many people question by interest in politics & presidential approval (for appendix)
byinterest <- svyby(~Q1, ~trumpapproval + highinterest, svy.people, svymean, keep.var=TRUE)
byinterest <- as.data.frame(byinterest)

fig3d <- ggplot(data=byinterest, aes(x=trumpapproval, y=Q1*100, fill=as.factor(highinterest))) +
  geom_bar(stat="identity", position=position_dodge())
fig3d <- fig3d + theme_bw() + scale_y_continuous(limits = c(0, 30))
fig3d <- fig3d + xlab("Trump approval") + ylab("Percent choosing wrong photo")
fig3d <- fig3d + scale_x_discrete(breaks=c(1,2,3,4,5),
                                  labels=c("Strongly \ndisapprove", "Somewhat \ndisapprove", "Not \nsure", "Somewhat \napprove", "Strongly \napprove"))
fig3d <- fig3d + scale_fill_manual(name="",
                                     breaks=c(0, 1),
                                     labels=c("Low interest", "High interest"),values=c("#999999","#000000"))
fig3d <- fig3d + geom_text(aes(label=paste(round(100*Q1,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")

# Collect figures to output Figure A1
setEPS()
postscript(file="figA1.eps", width=12, height=4)
grid.arrange(fig3a, fig3d, ncol=2)
dev.off()



# Graph which inauguration question by education & presidential vote
byeduc <- svyby(~Q2, ~presvote16x + educ2, svy.whichprez, svymean, keep.var=TRUE)
byeduc <- as.data.frame(byeduc)

fig4b <- ggplot(data=byeduc, aes(x=presvote16x, y=Q2*100, fill=as.factor(educ2))) +
  geom_bar(stat="identity", position=position_dodge())
fig4b <- fig4b + theme_bw() + scale_y_continuous(limits = c(0, 50))
fig4b <- fig4b + xlab("2016 Presidential Vote") + ylab("Percent choosing wrong inauguration")
fig4b <- fig4b + scale_x_discrete(breaks=c(4,1,2),
                                labels=c("Non- \nvoters", "Clinton \nVoters", "Trump \nVoters"))
fig4b <- fig4b + scale_fill_manual(name="",
                                   breaks=c(1, 2),
                                   labels=c("Low education", "High education"), values=c("#999999","#000000"))
fig4b <- fig4b + geom_text(aes(label=paste(round(100*Q2,0),"%",sep="")), vjust=-0.5, 
                         position=position_dodge(.9),color="black")


# Graph which inauguration question by interest in politics & presidential vote
byeduc <- svyby(~Q2, ~trumpapproval + educ2, svy.whichprez, svymean, keep.var=TRUE)
byeduc <- as.data.frame(byeduc)

fig4c <- ggplot(data=byeduc, aes(x=trumpapproval, y=Q2*100, fill=as.factor(educ2))) +
  geom_bar(stat="identity", position=position_dodge())
fig4c <- fig4c + theme_bw() + scale_y_continuous(limits = c(0, 50))
fig4c <- fig4c + xlab("Trump approval") + ylab("Percent choosing wrong inauguration")
fig4c <- fig4c + scale_x_discrete(breaks=c(1,2,3,4,5),
                                  labels=c("Strongly \ndisapprove", "Somewhat \ndisapprove", "Not \nsure", "Somewhat \napprove", "Strongly \napprove"))
fig4c <- fig4c + scale_fill_manual(name="",
                                     breaks=c(1, 2),
                                     labels=c("Low education", "High education"), values=c("#999999","#000000"))
fig4c <- fig4c + geom_text(aes(label=paste(round(100*Q2,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")

# Collect figures to output Figure 4
setEPS()
postscript(file="fig4.eps", width=12, height=4)
grid.arrange(fig4b, fig4c, ncol=2)
dev.off()


# Graph which inauguration question by interest in politics & presidential vote
byinterest <- svyby(~Q2, ~presvote16x + highinterest, svy.whichprez, svymean, keep.var=TRUE)
byinterest <- as.data.frame(byinterest)

fig4a <- ggplot(data=byinterest, aes(x=presvote16x, y=Q2*100, fill=as.factor(highinterest))) +
  geom_bar(stat="identity", position=position_dodge())
fig4a <- fig4a + theme_bw() + scale_y_continuous(limits = c(0, 50))
fig4a <- fig4a + xlab("2016 Presidential Vote") + ylab("Percent choosing wrong inauguration")
fig4a <- fig4a + scale_x_discrete(breaks=c(4,1,2),
                                  labels=c("Non- \nvoters", "Clinton \nVoters", "Trump \nVoters"))
fig4a <- fig4a + scale_fill_manual(name="",
                                   breaks=c(0, 1),
                                   labels=c("Low interest", "High interest"), values=c("#999999","#000000"))
fig4a <- fig4a + geom_text(aes(label=paste(round(100*Q2,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")

# Graph how many people question by interest in politics & presidential approval (for appendix)
byinterest <- svyby(~Q2, ~trumpapproval + highinterest, svy.whichprez, svymean, keep.var=TRUE)
byinterest <- as.data.frame(byinterest)
svychisq(~Q2+highinterest, design = subset(svy.whichprez, trumpapproval==5), statistic="adjWald")


fig4d <- ggplot(data=byinterest, aes(x=trumpapproval, y=Q2*100, fill=as.factor(highinterest))) +
  geom_bar(stat="identity", position=position_dodge())
fig4d <- fig4d + theme_bw() + scale_y_continuous(limits = c(0, 50))
fig4d <- fig4d + xlab("Trump approval") + ylab("Percent choosing wrong inauguration")
fig4d <- fig4d + scale_x_discrete(breaks=c(1,2,3,4,5),
                                  labels=c("Strongly \ndisapprove", "Somewhat \ndisapprove", "Not \nsure", "Somewhat \napprove", "Strongly \napprove"))
fig4d <- fig4d + scale_fill_manual(name="",
                                     breaks=c(0, 1),
                                     labels=c("Low interest", "High interest"), values=c("#999999","#000000"))
fig4d <- fig4d + geom_text(aes(label=paste(round(100*Q2,0),"%",sep="")), vjust=-0.5, 
                           position=position_dodge(.9),color="black")

# Collect figures to output Figure A2
setEPS()
postscript(file="figA2.eps", width=12, height=4)
grid.arrange(fig4a, fig4d, ncol=2)
dev.off()



